package com.livestocksolutions.intent;

import java.util.ArrayList;
import java.util.Date;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;

import com.livestocksolutions.Bovine;
import com.livestocksolutions.LivestockSolutions;
import com.livestocksolutions.R;
import com.livestocksolutions.ui.ListCattleArrayAdapter;
import com.livestocksolutions.ui.ViewCattleArrayAdapter;
import com.livestocksolutions.util.StringArrayParcelable;

/* -- Version History --
 *  1.0 : 19 September 2012 - Initial version.
 */

/**
 * A class to create the layout for a Search
 * @author Adam Parr
 * @version 1.0
 * 
 * @.modified 19 September 2012
 */
public class SearchActivity extends BaseActivity {
	public static final int SEARCHVIEW 		 = 0;
	
	/**
	 * Stores the reference to the XML layout template for each view
	 * @see #SEARCHVIEW
	 */
	public static final Integer[] RESOURCEID = new Integer[]{
		R.layout.list_single
	};
	/**
	 * Constructor
	 */
	public SearchActivity() {
		super(RESOURCEID);
	}
	
	/**
	 * Data related to this Search.
	 */
	protected ListCattleArrayAdapter cattleList;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(stack.size()==0){
    		setContentView(RESOURCEID[SEARCHVIEW]);
    		if(!built){
		        String search = currentState.getString(SearchManager.QUERY);
		        // Search for matching Bovine
		        ArrayList<String[]> barray = new ArrayList<String[]>();
		        Bovine[] bovineList = LivestockSolutions.getBovines();
		        
		        for(Bovine b : bovineList){
		        	if(b.getEartag().getEartagID().toLowerCase().contains(search.toLowerCase())
		        	|| b.getEartag().getNlisID().toLowerCase().contains(search.toLowerCase())
		        	|| b.getEartag().getRfID().toLowerCase().contains(search.toLowerCase())){
		        		String[] s = new String[ListCattleArrayAdapter.SIZE];
		        		Date d = b.getDob();
		        		s[ViewCattleArrayAdapter.ID] =  String.valueOf(b.getID());
		        		s[ViewCattleArrayAdapter.VISUALID] = b.getEartag().getEartagID();
		        		s[ViewCattleArrayAdapter.NLISRFID] = b.getEartag().getRfID();
		        		s[ViewCattleArrayAdapter.DOB] = d!=null?String.valueOf(d):"";
		        	
		        		barray.add(s);
		        	}
		        }
		        
		        cattleList = new ListCattleArrayAdapter(this,barray.toArray(new String[][]{}));
		    	setPage();
	            
				currentState.putInt(STATEID,SEARCHVIEW);
				built=true;
    		}
        }
    }
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		this.menu = menu;
        return false;
    }
	
    @Override
    public void onSaveInstanceState (Bundle outState) {
    	super.onSaveInstanceState (outState);
    	
    	outState.putParcelable("searchList", new StringArrayParcelable().setStrings(cattleList.get()));
    }
    
    @Override
    public void onRestoreInstanceState(Bundle inState) {
    	cattleList = new ListCattleArrayAdapter(this,((StringArrayParcelable)inState.getParcelable("searchList")).getStrings());

    	super.onRestoreInstanceState (inState);
    }
	
	@Override
	public boolean constructFrame(int view) {
		switch (view) {
			case SEARCHVIEW:
				setContentView(RESOURCEID[SEARCHVIEW]);
				setPage();
				break;
			default:
				return false;
		}
		return true;
	}
	
	protected void setPage(){
    	setupListTitle(R.id.selectiveListTitle, R.layout.list_item_cattle);
    	
    	LinearLayout view = (LinearLayout) findViewById(R.id.listView);
    	view.removeAllViews();
    	if(cattleList.get().length>0){
	    	cattleList.setRoot(view);
	    	for(int x=0; x< cattleList.getCount(); x++){
	    		View v = cattleList.getView(x, null, view);
	    		v.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(SearchActivity.this, BovineActivity.class);
						intent.putExtra(ITEMID, Long.parseLong(cattleList.getItem(v.getId())[ViewCattleArrayAdapter.ID]));
				    	startActivityForResult(intent, EXISTING);
					}
				});
	    		view.addView(v,x);
	    	}
    	}else{
    		view.addView(inflateLayout(R.layout.item_none,view));
    	}
	}
}
